//
//  RMMLoginView.m
//  Romomo
//
//  Created by puman on 16/10/18.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMLoginView.h"


@implementation RMMLoginView
{
    UIView *_backView;
}
- (void)dealloc{
    NS_REM(UIKeyboardWillShowNotification, nil);
    NS_REM(UIKeyboardWillHideNotification, nil);
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        NS_ADD(keyShow:, UIKeyboardWillShowNotification, nil);
        NS_ADD(keyHide:, UIKeyboardWillHideNotification, nil);
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.width / 2 - 100, 44, 200, 140)];
        imageV.image = [UIImage imageNamed:@"loog"];
        [self addSubview:imageV];
        
        _backView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWideth / 2 - 140, imageV.bottom + 24, 280, 99)];
        _backView.layer.cornerRadius = 5;
        _backView.layer.borderWidth = 1;
        _backView.layer.borderColor = RGBA(239, 239, 239, 1).CGColor;
        _backView.backgroundColor = RGBA(249, 249, 249, 1);
        [self addSubview:_backView];
        
        NSArray *imageName = @[@"1@3x",@"2@3x"];
        NSArray *plArray = @[@"请输入手机号码",@"请输入密码"];

        for (int i = 0; i < 2; i++) {
            UIImageView *imageViews = [[UIImageView alloc] initWithFrame:CGRectMake(12, 12 + 49 * i, 25, 25)];
            imageViews.image = [UIImage imageNamed:imageName[i]];
            [_backView addSubview:imageViews];
            
            UITextField *textF = [[UITextField alloc] initWithFrame:CGRectMake(imageViews.right + kRow, imageViews.top, _backView.width - 47 - imageViews.right, imageViews.height)];
            textF.placeholder = plArray[i];
            textF.font = kFont(16);
            textF.clearButtonMode = UITextFieldViewModeWhileEditing;
            [_backView addSubview:textF];
            if (i == 0) {
                _userName = textF;
                UIButton *button =  [UIButton buttonWithType:(UIButtonTypeCustom)];
                button.frame = CGRectMake(0, 0, kScreenWideth, 40);
                [button setTitle:@"完成" forState:(UIControlStateNormal)];
                [button setTitleColor:kBasicColor forState:(UIControlStateNormal)];
                BUTTON_S(button, doneBut);
                button.backgroundColor = kWhiteColor;
                button.layer.borderColor = RGBA(198, 197, 198, 1).CGColor;
                button.layer.borderWidth = 1;
                _userName.inputAccessoryView= button;

            } else {
                _password = textF;
            }
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_backView.width - 37, 12 + 49 * i, 25, 25)];
            imageView.hidden = YES;
            imageView.image = [UIImage imageNamed:@"3@3x"];
            [_backView addSubview:imageView];
            
            UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 49, _backView.width, 1)];
            lineLabel.backgroundColor = RGBA(239, 239, 239, 1);
            [_backView addSubview:lineLabel];
            
        }
        _tiShiLabel = [[UILabel alloc] initWithFrame:CGRectMake(_backView.left, _backView.bottom, _backView.width, 40)];
        _tiShiLabel.hidden = YES;
        _tiShiLabel.textColor = COLORFROMCODE(0xF95B45, 1);
        _tiShiLabel.font = kFont(14);
        _tiShiLabel.text = @"手机号或密码输入错误,请重新输入";
        _tiShiLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_tiShiLabel];
        
        _loginBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _loginBut.frame = CGRectMake(_tiShiLabel.left, _tiShiLabel.bottom, _tiShiLabel.width, 44);
        _loginBut.backgroundColor = kWhiteColor;
        [_loginBut setTitleColor:COLORFROMCODE(0xD7D7D7 , 1) forState:(UIControlStateNormal)];
        _loginBut.titleLabel.font = kBFont(14);
        _loginBut.layer.masksToBounds = YES;
        _loginBut.layer.cornerRadius = 5;
        _loginBut.layer.borderColor = COLORFROMCODE(0xF3F2F3 , 1).CGColor;
        _loginBut.layer.borderWidth = 1;
        _loginBut.userInteractionEnabled = NO;
        [_loginBut setTitle:@"登录" forState:(UIControlStateNormal)];
        [self addSubview:_loginBut];
        
        _forgetPasswordBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _forgetPasswordBut.frame = CGRectMake(_loginBut.right- 70, _loginBut.bottom, 70, kMaric + kRow);
        _forgetPasswordBut.backgroundColor = kWhiteColor;
        [_forgetPasswordBut setTitleColor:kBasicColor forState:(UIControlStateNormal)];
        _forgetPasswordBut.titleLabel.font = kBFont(14);
        [_forgetPasswordBut setTitle:@"忘记密码" forState:(UIControlStateNormal)];
        [self addSubview:_forgetPasswordBut];


        NSArray *array = @[@"  密码登录",@"  微信登录",@"  QQ登录"];
        NSArray *iArray = @[@"4@3x",@"6@3x",@"5@3x"];

        for (int i = 0; i < 1; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake((self.width - 150) / 2, self.height - 52, 150, kMaric);
            button.tag = 10 + i;
            [button setTitle:array[i] forState:(UIControlStateNormal)];
            [button setTitleColor:COLORFROMCODE(0xBFBFBF, 1) forState:(UIControlStateNormal)];
            [button setImage:[UIImage imageNamed:iArray[i]] forState:(UIControlStateNormal)];
            button.titleLabel.font = kFont(13);
           // [self addSubview:button];
            if (i < 2) {
                UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(button.right, button.top + 2, 1, button.height - 4)];
                lineLabel.backgroundColor = COLORFROMCODE(0xBFBFBF, 1);
               // [self addSubview:lineLabel];
            }
        }
       _procotolBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _procotolBut.frame = CGRectMake((self.width - 120) / 2, self.height - 52, 120, kMaric);
        [_procotolBut setTitle:@"容么么产品使用协议" forState:(UIControlStateNormal)];
        [_procotolBut setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
        _procotolBut.titleLabel.font = kFont(13);
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(_procotolBut.left, _procotolBut.bottom, _procotolBut.width, 1)];
        view.backgroundColor = [UIColor blueColor];
        [self addSubview:view];
        [self addSubview:_procotolBut];

    }
    return self;
}
- (void)doneBut{
    [_userName resignFirstResponder];
}
- (void)keyShow:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect rect = [value CGRectValue];
    CGFloat keyBoardHeight = rect.size.height;
    CGFloat height = keyBoardHeight - (kScreenHeight - 64 - _backView.bottom);
    if (height > 0) {
        [UIView animateWithDuration:kInterval animations:^{
            self.frame = CGRectMake(0, - height, kScreenWideth, kScreenHeight - 64);
        } completion:^(BOOL finished) {
            
        }];
    }

}
- (void)keyHide:(NSNotification *)notification{
    if (self.top < 0) {
        [UIView animateWithDuration:kInterval animations:^{
            self.frame = CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64);
        } completion:^(BOOL finished) {
            
        }];

    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
